<template>
  <div class="container">
    <!-- 主要内容 -->
    <el-row type="flex" justify="center" align="middle" class="main">
      <div class="form-wrapper">
        <!-- 表单头部tab -->
        <el-row type="flex" justify="center" class="tabs">
          <span
            :class="{ active: currentTab === index }"
            v-for="(item, index) in [`登录`, `注册`]"
            :key="index"
            @click="handleChangeTab(index)"
          >
            {{ item }}
          </span>
        </el-row>

        <!-- 登录功能 -->
        <div v-if="currentTab == 0">
          <el-form
            :model="loginForm"
            ref="loginForm"
            :rules="rules"
            class="form"
          >
            <el-form-item class="form-item" prop="username">
              <el-input placeholder="用户名/手机" v-model="loginForm.username">
              </el-input>
            </el-form-item>

            <el-form-item class="form-item" prop="password">
              <el-input
                placeholder="密码"
                type="password"
                v-model="loginForm.password"
              >
              </el-input>
            </el-form-item>

            <p class="form-text">
              <nuxt-link to="#">忘记密码</nuxt-link>
            </p>

            <el-button class="submit" type="primary" @click="handleLoginSubmit">
              登录
            </el-button>
          </el-form>
        </div>

        <!-- 注册功能 -->
        <div v-if="currentTab == 1">
          <el-form
            :model="registerForm"
            ref="registerForm"
            :rules="rules"
            class="form"
          >
            <el-form-item class="form-item" prop="username">
              <el-input
                placeholder="用户名手机"
                v-model="registerForm.username"
              >
              </el-input>
            </el-form-item>

            <el-form-item class="form-item" prop="captcha">
              <el-input placeholder="验证码" v-model="registerForm.captcha">
                <template slot="append">
                  <el-button @click="handleSendCaptcha"> 发送验证码 </el-button>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item class="form-item" prop="nickname">
              <el-input placeholder="你的名字" v-model="registerForm.nickname">
              </el-input>
            </el-form-item>

            <el-form-item class="form-item" prop="password">
              <el-input
                placeholder="密码"
                type="password"
                v-model="registerForm.password"
              ></el-input>
            </el-form-item>

            <el-form-item class="form-item" prop="checkPassword">
              <el-input
                placeholder="确认密码"
                type="password"
                v-model="registerForm.checkPassword"
              >
              </el-input>
            </el-form-item>

            <el-button class="submit" type="primary" @click="handleRegSubmit">
              注册
            </el-button>
          </el-form>
        </div>
      </div>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value == "") {
        // 输出错误提示
        callback(new Error("请再次输入密码"));
      } else if (value != this.form.password) {
        // 输出错误提示
        callback(new Error("两次密码必须一致"));
      } else {
        // 输出成功回调
        callback();
      }
    };
    return {
      currentTab: 0,

      loginForm: {
        username: "13800138000",
        password: "123456",
      },
      registerForm: {
        username: "",
        nickname: "",
        captcha: "",
        password: "",
        checkPassword: "",
      },
      // 表单规则
      rules: {
        // 这里是一个对象
        // 以 key: value 的形式指定每个字段应该符合的规则
        username: [
          // 这里是规则数组, 里面每个元素都是一条需要校验的 rule
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入11位合法手机号",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            message: "密码必须6位以上",
            trigger: "blur",
          },
        ],
        nickname: [
          {
            required: true,
            message: "该项不能为空",
            trigger: "blur",
          },
        ],
        captcha: [
          {
            required: true,
            message: "该项不能为空",
            trigger: "blur",
          },
        ],
        checkPassword: [
          {
            validator: validatePass,
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    currentTab() {
      if (this.$refs.loginForm) {
        this.$refs.loginForm.clearValidate();
      }
      if (this.$refs.registerForm) {
        this.$refs.registerForm.clearValidate();
      }
    },
  },
  methods: {
    handleChangeTab(index) {
      this.currentTab = index;
    },
    // 登录
    async handleLoginSubmit() {
      const pass = await this.$refs.loginForm.validate();
      if (pass) {
        this.$store.dispatch("user/login", this.loginForm).then((res) => {
          this.$message.success("登录成功");
          console.log(res);
          this.$emit("closeDialog");
        });
      }
    },
    // 注册
    async handleRegSubmit() {
      const pass = await this.$refs.registerForm.validate();
      if (pass) {
        this.$store.dispatch("user/register", this.registerForm).then((res) => {
          this.$message.success("注册成功");
          this.$emit("closeDialog");
        });
      }
    },

    // 发送验证码
    handleSendCaptcha() {
      this.$axios({
        method: "post",
        url: "/captchas",
        data: {
          tel: this.form.username,
        },
      }).then((res) => {
        console.log(res.data);
        if (res.data.code) {
          this.$message.success("验证码发送成功: " + res.data.code);
        }
      });
    },
  },
};
</script>

<style scoped lang="less">
.container {
  .main {
    width: 100%;
    margin: 0 auto;
    position: relative;

    .form-wrapper {
      width: 400px;
      margin: 0 auto;
      background: #fff;
      border: 1px solid #eee;
      overflow: hidden;

      .tabs {
        span {
          display: block;
          width: 50%;
          height: 50px;
          box-sizing: border-box;
          border-top: 2px #eee solid;
          background: #eee;
          line-height: 48px;
          text-align: center;
          cursor: pointer;
          color: #666;

          &.active {
            color: orange;
            border-top-color: orange;
            background: #fff;
            font-weight: bold;
          }
        }
      }
    }
  }
  .form {
    padding: 25px;
  }

  .form-item {
    margin-bottom: 20px;
  }

  .form-text {
    font-size: 12px;
    color: #409eff;
    text-align: right;
    line-height: 1;
  }

  .submit {
    width: 100%;
    margin-top: 10px;
  }
}
</style>